package com.ruoyi.web.controller.mall.goodsGoods.service.impl;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.web.controller.mall.goodsGoods.domain.GoodsGoods;
import com.ruoyi.web.controller.mall.goodsGoods.mapper.GoodsGoodsMapper;
import com.ruoyi.web.controller.mall.goodsGoods.service.IGoodsGoodsService;
import com.ruoyi.web.controller.mall.goodsImages.domain.GoodsImages;
import com.ruoyi.web.controller.mall.goodsImages.mapper.GoodsImagesMapper;
import com.ruoyi.web.controller.mall.goodsProduct.domain.GoodsProduct;
import com.ruoyi.web.controller.mall.goodsProduct.mapper.GoodsProductMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * 商品Service业务层处理
 * 
 * @author ruoyi
 * @date 2021-01-18
 */
@Service
public class GoodsGoodsServiceImpl implements IGoodsGoodsService 
{
    @Autowired
    private GoodsGoodsMapper goodsGoodsMapper;
    @Autowired
    private GoodsProductMapper goodsProductMapper;
    @Autowired
    private GoodsImagesMapper goodsImagesMapper;

    /**
     * 查询商品
     * 
     * @param id 商品ID
     * @return 商品
     */
    @Override
    public GoodsGoods selectGoodsGoodsById(String id)
    {
        return goodsGoodsMapper.selectGoodsGoodsById(id);
    }

    /**
     * 查询商品列表
     * 
     * @param goodsGoods 商品
     * @return 商品
     */
    @Override
    public List<GoodsGoods> selectGoodsGoodsList(GoodsGoods goodsGoods)
    {
        return goodsGoodsMapper.selectGoodsGoodsList(goodsGoods);
    }

    @Override
    public List<Map<String, Object>> queryList(GoodsGoods goodsGoods) {
        return goodsGoodsMapper.queryList(goodsGoods);
    }

    /**
     * 新增商品
     * 
     * @param goodsGoods 商品
     * @return 结果
     */
    @Override
    public int insertGoodsGoods(GoodsGoods goodsGoods)
    {
        return goodsGoodsMapper.insertGoodsGoods(goodsGoods);
    }

    @Override
    @Transactional
    public int addGoodsGoods(GoodsGoods goodsGoods) {
        String goodsId = StringUtils.get32UUID();
        goodsGoods.setId(goodsId);
        goodsGoods.setGmtCreate(DateUtils.getNowDate());
        goodsGoods.setDelFlag(0);
        goodsGoods.setNumSale(0);
        goodsGoodsMapper.insertGoodsGoods(goodsGoods);
        //添加产品
        List<GoodsProduct>  productList = goodsGoods.getProductList();
        if (productList!=null&&productList.size()>0){
            int i =0;
            for (GoodsProduct gp:productList){
                gp.setId(StringUtils.get32UUID());
                gp.setGoodsId(goodsId);
                gp.setDelFlag(0);
                gp.setGmtCreate(DateUtils.getNowDate());
                gp.setLocation(i);
                goodsProductMapper.insertGoodsProduct(gp);
                i++;
            }
        }
        //添加轮播图片
        List<GoodsImages>  bannerList = goodsGoods.getBannerList();
        if (bannerList!=null&&bannerList.size()>0){
            int i =0;
            for (GoodsImages gi:bannerList){
                gi.setId(StringUtils.get32UUID());
                gi.setGoodsId(goodsId);
                gi.setDelFlag(0);
                gi.setGmtCreate(DateUtils.getNowDate());
                gi.setLocation(i);
                gi.setAddress(0);
                goodsImagesMapper.insertGoodsImages(gi);
                i++;
            }
        }
        //添加详情图片
        List<GoodsImages>  detailImgList = goodsGoods.getDetailImgList();
        if (detailImgList!=null&&detailImgList.size()>0){
            int i =0;
            for (GoodsImages gi:detailImgList){
                gi.setId(StringUtils.get32UUID());
                gi.setGoodsId(goodsId);
                gi.setDelFlag(0);
                gi.setAddress(1);
                gi.setGmtCreate(DateUtils.getNowDate());
                gi.setLocation(i);
                goodsImagesMapper.insertGoodsImages(gi);
                i++;
            }
        }
        return 1;
    }

    /**
     * 修改商品
     * 
     * @param goodsGoods 商品
     * @return 结果
     */
    @Override
    public int updateGoodsGoods(GoodsGoods goodsGoods)
    {
        return goodsGoodsMapper.updateGoodsGoods(goodsGoods);
    }

    @Override
    @Transactional
    public int editGoodsGoods(GoodsGoods goodsGoods) {
        goodsGoods.setGmtModified(DateUtils.getNowDate());
        goodsGoodsMapper.updateGoodsGoods(goodsGoods);
        //修改产品
        goodsProductMapper.removeByGoodsId(goodsGoods.getId());
        List<GoodsProduct>  productList = goodsGoods.getProductList();
        if (productList!=null&&productList.size()>0){
            int i =0;
            for (GoodsProduct gp:productList){
                gp.setGoodsId(goodsGoods.getId());
                gp.setGmtModified(DateUtils.getNowDate());
                gp.setLocation(i);
                gp.setDelFlag(0);
                if (gp.getId()==null||"".equals(gp.getId())){
                    gp.setId(StringUtils.get32UUID());
                    gp.setGmtCreate(DateUtils.getNowDate());
                    goodsProductMapper.insertGoodsProduct(gp);
                }else {
                    goodsProductMapper.updateGoodsProduct(gp);
                }
                i++;
            }
        }
        //修改轮播图片
        goodsImagesMapper.removeByGoodsId(goodsGoods.getId());
        List<GoodsImages>  bannerList = goodsGoods.getBannerList();
        if (bannerList!=null&&bannerList.size()>0){
            int i =0;
            for (GoodsImages gi:bannerList){

                gi.setGoodsId(goodsGoods.getId());
                gi.setDelFlag(0);
                gi.setLocation(i);
                gi.setAddress(0);
                if (gi.getId()==null||"".equals(gi.getId())){
                    gi.setId(StringUtils.get32UUID());
                    gi.setGmtCreate(DateUtils.getNowDate());
                    goodsImagesMapper.insertGoodsImages(gi);
                }else {
                    goodsImagesMapper.updateGoodsImages(gi);
                }
                i++;
            }
        }
        //修改详情图片
        List<GoodsImages>  detailImgList = goodsGoods.getDetailImgList();
        if (detailImgList!=null&&detailImgList.size()>0){
            int i =0;
            for (GoodsImages gi:detailImgList){
                gi.setGoodsId(goodsGoods.getId());
                gi.setDelFlag(0);
                gi.setAddress(1);
                gi.setLocation(i);
                if (gi.getId()==null||"".equals(gi.getId())){
                    gi.setId(StringUtils.get32UUID());
                    gi.setGmtCreate(DateUtils.getNowDate());
                    goodsImagesMapper.insertGoodsImages(gi);
                }else {
                    goodsImagesMapper.updateGoodsImages(gi);
                }
                i++;
            }
        }
        return 1;
    }

    /**
     * 删除商品对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteGoodsGoodsByIds(String[] ids)
    {
        return goodsGoodsMapper.deleteGoodsGoodsByIds(ids);
    }

    /**
     * 删除商品信息
     * 
     * @param id 商品ID
     * @return 结果
     */
    @Override
    @Transactional
    public int deleteGoodsGoodsById(String id)
    {
        goodsGoodsMapper.deleteGoodsGoodsById(id);
        goodsImagesMapper.removeByGoodsId(id);
        goodsProductMapper.removeByGoodsId(id);
        return 1;
    }
}
